<?php
namespace app\api\controller;

use controller\BasicAppApi;
use think\Db;

/**
 * 活动控制器
 * class Activity
 * @package app\api\controller
 * @author 骆东 <576664583@qq.com>
 * @date 2017年8月28日14:59:28
 */
class Activity extends BasicAppApi{

    /**
     * 控制器基础方法
     */
    public function _initialize(){
        
    }

    /**
     * 控制器
     * @return string
     */
    public function index(){

    }


    /**
     * 新增活动
     *
     * @return void
     */
    public function add(){
        $param = $this->request->param();
        if($param['id'] != 0){ //如果id 不等于0则更新数据，否则新建数据
            unset($param['token']);
            $data['content'] = json_encode($param);
            $data['title']   = $param['title'];
            $data['startTime']   = $param['startTime'];
            $data['endTime']   = $param['endTime'];
            $update = Db::name('AppActivity')->where('id',$param['id'])->update($data);
            if($update){
                return json(
                    [
                    'code' => 200,
                    'msg'  => '更新成功',
                    'data' => $param
                    ]
                );
            }else{
                return json(['code' => 404,'msg'  => '更新成功',]);
            }
        }else{
            $user = Db::name('AppUser')->where('token',$param['token'])->find();
            if($user){
                $data['userId'] = $user['id'];
                unset($param['token']);
                $data['content'] = json_encode($param);
                $data['title']   = $param['title'];
                $data['startTime']   = $param['startTime'];
                $data['endTime']   = $param['endTime'];
                $activity = Db::name('AppActivity')->insertGetId($data);
                if($activity){
                    $param['id'] = $activity;
                    $update = Db::name('AppActivity')->where('id',$activity)->update(['content'=>json_encode($param)]);
                    return json(
                        [
                        'code' => 200,
                        'msg'  => '新增成功',
                        'data' => $param
                        ]
                    );
                }else{
                    return json(['code' => 404,'msg'  => '新增失败',]);
                }

            }else{
                return json(['code' => 404,'msg'  => 'token失效，请从新登录',]);
            }
        }
    }

    /**
     * 获取我的活动列表
     *
     * @return json
     */
    public function myList(){
        $param = $this->request->param();
        $activity = Db::name('AppActivity')->where('userId',$param['uid'])->select();
        if($activity){
            return json(
                [
                'code' => 200,
                'msg'  => '成功',
                'data' => $activity
                ]
            );
        }else{
            return json(['code' => 404,'msg'  => '失败',]);
        }
    }
    
    /**
     * 获取活动详情
     *
     * @return json
     */
    public function detail(){
        $param = $this->request->param();
        $activity = Db::name('AppActivity')->where('id',$param['id'])->find();
        if($activity){
            return json(
                [
                'code' => 200,
                'msg'  => '成功',
                'data' => $activity
                ]
            );
        }else{
            return json(['code' => 404,'msg'  => '失败',]);
        }
    }

}
